package com.google.gwt.demo.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Composite;

public abstract class AbstractComposite<P extends AbstractComposite.Presenter> extends Composite {

	public interface Presenter extends WidgetPresenter {

	}

	private P presenter;

	public P getPresenter(){
		return presenter;
	}

	public void setPresenter(P presenter){
		this.presenter = presenter;
		initWidget();
	}

	public AbstractComposite(){
		this.presenter = getDefaultPresenter();
		initWidget();
	}
	
	public AbstractComposite(P presenter){
		this.presenter = presenter;
		initWidget();
	}
	
	protected abstract void initWidget();
	
	protected abstract P getDefaultPresenter();
	
	protected void update(){
		getPresenter().update();
	}
	
	protected void autoUpdate(){
		if(!GWT.isProdMode()){
	 		update();
		}
	}
	
	@Override
	public void onAttach(){
		super.onAttach();
		getPresenter().onWidgetAttach();
	}
	
	@Override
	public void onDetach(){
		super.onDetach();
		getPresenter().onWidgetDetach();
	}
	
	@Override
	public void onBrowserEvent(Event event){
		getPresenter().onBrowserEvent(event);
	}
}